package top.suven.base.http.admin.old.controller;


import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import top.suven.base.common.excel.ColumnBox;
import top.suven.base.common.excel.ExportUtils;
import top.suven.base.core.mybatis.PageData;
import top.suven.base.http.admin.frame.config.ResponseResult;
import top.suven.base.http.admin.old.model.SysAdmin;
import top.suven.base.http.admin.old.service.SysAdminService;
import top.suven.base.http.admin.old.vo.request.SysAdminRequestVo;
import top.suven.base.http.admin.old.vo.response.SysAdminResponseVo;
import top.suven.base.http.admin.old.vo.valid.OldValidGroup;
import top.suven.base.http.enums.EnableStatusEnum;
import top.suven.base.http.enums.StatusEnum;
import top.suven.base.http.enums.SysMsgEnumType;
import top.suven.base.util.utilize.BeanUtil;
import top.suven.base.util.utilize.DateUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**   
 * @Title: SysAdminController.java
 * @Description: 管理员信息的控制服务类
 * @author lixiangling
 * @date   2018/08/07 14:49:17
 * @version V1.0
 * ----------------------------------------------------------------------------
 *  modifyer    modifyTime                 comment
 *
 * ----------------------------------------------------------------------------
 *
 */
@Controller
@RequestMapping("/config/sysAdmin")
public class SysAdminController {
    private final static Logger logger = LoggerFactory.getLogger(SysAdminController.class);
	@Autowired
	private SysAdminService sysAdminService;
    /**
     * @Title: 跳转到管理员信息主界面
     * @Description:sysAdminRequestVo @{Link SysAdminRequestVo}
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/07 14:49:17
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @RequestMapping(value = "")
    public String index(){
        return "old/sysAdmin_index";
    }
    /**
     * @Title: 获取管理员信息分页信息
     * @Description:sysAdminRequestVo @{Link SysAdminRequestVo}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/07 14:49:17
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @ResponseBody
    @RequestMapping(value = "list")
    public PageData<SysAdminResponseVo> list(@Validated(value = {OldValidGroup.OldQueryGroup.class}) SysAdminRequestVo sysAdminRequestVo){
        SysAdmin sysAdmin = new SysAdmin();
        BeanUtil.copyProperties(sysAdminRequestVo, sysAdmin);
        return sysAdminService.getSysAdminPage(sysAdmin, sysAdminRequestVo.getPage(), sysAdminRequestVo.getLimit());
    }
   /**
    * @Title: 保存管理员信息信息
    * @Description:sysAdminRequestVo @{Link SysAdminRequestVo}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/07 14:49:17
    *  --------------  ------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "save")
    public ResponseResult save(@Validated(value = {OldValidGroup.OldFormGroup.class}) SysAdminRequestVo sysAdminRequestVo){
        SysAdmin sysAdmin = new SysAdmin();
        BeanUtil.copyProperties(sysAdminRequestVo, sysAdmin);
        sysAdmin.setModifyTime(new Date());
        if(sysAdmin.getAdminCode() != null){
            sysAdminService.updSysAdmin(sysAdmin);
        }else{
            sysAdmin.setCreateTime(sysAdmin.getModifyTime());
            sysAdmin.setStatus(StatusEnum.VALID.getIndex());
            sysAdmin.setEnableStatus(EnableStatusEnum.ENABLE.getIndex());
            sysAdminService.addSysAdmin(sysAdmin);
        }
        return ResponseResult.ok();
    }
   /**
    * @Title: 查看管理员信息信息
    * @Description:sysAdminRequestVo @{Link SysAdminRequestVo}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/07 14:49:17
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping(value = "detail")
    public String detail(ModelMap modelMap, String id){
        SysAdmin sysAdmin = null;
        if(StringUtils.isNotBlank(id)){
            sysAdmin = sysAdminService.getSysAdmin(id);
        }
        modelMap.put("sysAdmin", sysAdmin);
        return "old/sysAdmin_detail";
    }
    /**
     * @Title: 跳转管理员信息编辑界面
     * @Description:id @{Link Long}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/07 14:49:17
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @RequestMapping(value = "edit")
    public String edit(ModelMap modelMap, String id){
        SysAdmin sysAdmin = null;
        if(StringUtils.isNotBlank(id)){
            sysAdmin = sysAdminService.getSysAdmin(id);
        }
        modelMap.put("sysAdmin", sysAdmin);
        return "old/sysAdmin_edit";
    }
    /**
     * @Title: 删除管理员信息信息
     * @Description:id @{Link Long}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/07 14:49:17
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @ResponseBody
    @RequestMapping("del")
    public ResponseResult del( @RequestParam(value = "id") Long[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        sysAdminService.del(ids);
        return ResponseResult.ok();
    }
   /**
    * @Title: 启用管理员信息信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/07 14:49:17
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "turnOn")
    public ResponseResult turnOn(@RequestParam(value = "id") Long[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        sysAdminService.turnOn(ids);
        return ResponseResult.ok();
    }
   /**
    * @Title: 禁用管理员信息信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/07 14:49:17
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "turnOff")
    public ResponseResult turnOff(@RequestParam(value = "id") Long[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        sysAdminService.turnOff(ids);
        return ResponseResult.ok();
    }

    /**
    * @Title: 导出管理员信息信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/07 14:49:17
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping("export")
    public void export(HttpServletResponse response, SysAdminRequestVo sysAdminRequestVo){
        SysAdmin sysAdmin = new SysAdmin();
        BeanUtil.copyProperties(sysAdminRequestVo, sysAdmin);
        PageData<SysAdminResponseVo> sysAdminPage = sysAdminService.getSysAdminPage(sysAdmin, sysAdminRequestVo.getPage(), sysAdminRequestVo.getLimit());
        List<SysAdminResponseVo> data = sysAdminPage.getData();

        ColumnBox columnBox = ColumnBox.create()
                ;

        try {
            ExportUtils.write(String.format("管理员信息_%s.xls",DateUtil.date("yyyy-MM-dd",new Date().getTime())), response, data, columnBox);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}